<?php

/**
 * a class to handle URLs
 * in fact, only their QUERY_STRING part
 *
 * everything else is unneeded for the moment
 *
 */
class Url
{
    /**
     * array of url GET parameters
     *
     * @var array
     */
    var $params    = array();

    /**
     * url fragment (#blah-blah-blah)
     *
     * @var string
     */
    var $fragment    = '';

    /**
     * a constructor
     *
     * @param string $string
     * @return Url
     */
    function Url($string = '')
    {
        $this->_parse($string);
    }

    /**
     * returns Url GET parameter(s)
     *
     * @param string $name
     * @return mixed
     */
    function getParam($name = null)
    {
        return is_null($name)
            ? $this->params
            : (isset($this->params[$name])
                ? $this->params[$name] : null);
    }

    /**
     * sets or unsets Url GET parameter
     *
     * @param string $name
     * @param string $value
     */
    function setParam($name, $value = null)
    {
        if ($value !== null)
        {
            $this->params[$name] = $value;
        }
        else
        {
            $this->unsetParam($name);
        }
    }

    /**
     * unsets Url GET parameter
     *
     * @param string $name
     */
    function unsetParam($name)
    {
        unset($this->params[$name]);
    }

    /**
     * returns Url fragment
     *
     * @return string
     */
    function getFragment()
    {
        return $this->fragment;
    }

    /**
     * sets Url fragment
     *
     * @param string $fragment
     */
    function setFragment($fragment)
    {
        $this->fragment = $fragment;
    }

    /**
     * generates Url string
     *
     * @return string
     */
    function toString()
    {
        $string = count($this->params) > 0 ? '?' : '';

        //
        //     compose GET parameter pairs
        //
        if (count($this->params) > 0)
        {
            $string = '?';

            $parts = array();

            foreach ($this->params as $name => $value)
            {
                $parts[] = $name . '=' . $value;
            }

            //
            //     append GET parameters
            //
            $string .= join('&', $parts);
        }
        else
        {
            $string = '';
        }

        //
        //     append fragment
        //
        if ($this->fragment != '')
        {
            $string .= '#' . $this->fragment;
        }

        return $string;
    }

    /**
      * parses fiven string into Url properties
     *
     * @param string $url
     * @access private
     */
    function _parse($url)
    {
        if (preg_match('/^\?([^#]*)(#(.*))?/', $url, $match))
        {
            foreach (explode('&', $match[1]) as $part)
            {
                $arr = explode('=', $part);

                $this->params[$arr[0]] = $arr[1];
            }

            $this->fragment = isset($match[3]) ? $match[3] : '';
        }
    }
}
